package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.ItemRefreshBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 私人定制（专题4张图片）
 * Created by wcy on 18/5/15.
 */
public class AdSelectedFiveLayout extends LinearLayout implements IRefresh<SelectedBean> {


    @BindView(R.id.label)
    TextView mLabel;
    @BindView(R.id.grid_container)
    GridLayout mGridContainer;
    ItemRefreshBean mItemRefreshBean;
    boolean mIsFirst;

    public AdSelectedFiveLayout(Context context) {
        super(context);
        init();
    }

    public AdSelectedFiveLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSelectedFiveLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_selected_five_layout, this);
        ButterKnife.bind(view);
    }

    @Override
    public void refresh(SelectedBean selectedBean, Object object) {
        ItemRefreshBean item = (ItemRefreshBean) object;
        if (selectedBean == null || item == null) {
            return;
        }
        mItemRefreshBean = item;
        mLabel.setText(item.selectionName);
        mLabel.setCompoundDrawablesWithIntrinsicBounds(item.selectionIcon, 0, 0, 0);
        if (!mIsFirst) {
            for (int i = 0; i < 4; i++) {
                GridLayout.LayoutParams params = (GridLayout.LayoutParams) mGridContainer.getChildAt(i).getLayoutParams();
                params.width = (DisplayUtils.getScreenWidth(mGridContainer.getContext()) - DisplayUtils.dip2px(mGridContainer.getContext(), 39)) / 2;
                mGridContainer.getChildAt(i).setLayoutParams(params);
                mGridContainer.getChildAt(i).setVisibility(View.GONE);
            }
            mIsFirst = true;
        }
        if (!Utils.isEmptyList(selectedBean.list)) {
            if (selectedBean.list.size() >= 4) {
                selectedBean.list = selectedBean.list.subList(0, 4);
            }
            for (int i = 0; i < selectedBean.list.size(); i++) {
                mGridContainer.getChildAt(i).setVisibility(VISIBLE);
                final int pos = i;
                mGridContainer.getChildAt(i).setOnClickListener(v ->{
                    Map<String, String> map = new HashMap<>();
                    map.put("title", selectedBean.list.get(pos).title);
                    map.put("id", selectedBean.list.get(pos).id);
                    ARouterUtils.toActivity(ARouterConfig.BOOK_MENU_DETAIL, map);
                    //埋点
                    BookCityUtils.takeIf(item,Constants.BookCityModuleType.TYPE_SElECTED_SIREMDINGZHI,()->{
                        AdPresenter.pvuvStatics(pos,
                                R.string.SCTUIJIAN5_POSITION1,
                                R.string.SCTUIJIAN5_POSITION2,
                                R.string.SCTUIJIAN5_POSITION3,
                                R.string.SCTUIJIAN5_POSITION4);
                    });
                });
                FrameLayout container = (FrameLayout) mGridContainer.getChildAt(i);
                ImageView imageView = (ImageView) container.getChildAt(0);
                TextView tv = (TextView) container.getChildAt(1);
                BookUtils.parseColor(tv, selectedBean.list.get(i).small_img_title_color);
                if (!TextUtils.isEmpty(selectedBean.list.get(i).small_img_title)) {
                    tv.setText(selectedBean.list.get(i).small_img_title);
                }
                GlideImageLoader.load(selectedBean.list.get(i).title_small_img, imageView, GlideImageLoader.getEmptyConfig());
            }
        }
    }

    @Override
    public void onError(int code, String desc) {

    }

    @Override
    public void reset() {

    }
}
